import { createApp } from 'vue'
import { createPinia } from 'pinia'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
 // 添加全局样式
import '@/styles/index.css'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import App from './App.vue'
import router from './router'
import { useUserStore } from '@/stores/user'

// 创建Vue实例
const app = createApp(App)
const pinia = createPinia()

// 先注册Pinia，再注册路由
app.use(pinia)
app.use(router)
app.use(ElementPlus, {
  locale: zhCn,
  size: 'default'
})

// 现在可以安全使用store
const userStore = useUserStore()

app.config.errorHandler = (err) => {
  console.error(err)
  router.push('/500')
}

// 自动登出检测
setInterval(() => {
  if (userStore.isLoggedIn) {
    const idleTime = Date.now() - userStore.lastActiveTime
    if (idleTime > 5 * 60 * 1000) { // 5分钟
      userStore.logout()
      router.push('/login')
    }
  }
}, 60 * 1000) // 每分钟检查一次

// 用户活动监听
document.addEventListener('click', () => userStore.updateActiveTime())
document.addEventListener('mousemove', () => userStore.updateActiveTime())
document.addEventListener('keypress', () => userStore.updateActiveTime())

// 挂载应用
app.mount('#app')